package com.example.demo.controller;

import com.example.demo.service.OllamaChatService;
import com.example.demo.utils.ImageString;
import com.example.demo.utils.Result; // 导入Result类
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.io.Serializable;

@RestController
@RequestMapping("/ingredient/analyze")
public class OllamaChatController {

    private final OllamaChatService chatService;

    public OllamaChatController(OllamaChatService chatService) {
        this.chatService = chatService;
    }

    @GetMapping
    public Mono chat() { // 修改返回类型为Mono<Result<String>>
        ImageString imageString = new ImageString();
        String prompt = "接下来你是一个食品健康助手，帮助我分析以上食品的添加剂"
                + imageString.getImageString()
                + "，请根据以上食品的成分分析健康程度";

        if (prompt.isBlank()) {
            return Mono.just(Result.error("错误：请求内容不能为空")); // 使用Result.error创建错误结果
        }

        return chatService.generate(prompt)
                .map(response -> Result.success(200,"分析成功",response)) // 成功时使用Result.success封装数据
                .onErrorResume(e ->
                        Mono.just(new Result<>(404, "服务暂不可用，错误信息：" + e.getMessage(), null))); // 错误时手动创建Result<String>对象
    }
}
